package com.kzj.mls.html5;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;


import com.kzj.mls.R;

import de.greenrobot.event.EventBus;

/**
 * A webview which support many Html5 feature like video ,audio etc.
 */
public class Html5Webview extends WebView {

	private Context mContext;
	private MyWebChromeClient mWebChromeClient;
	private View mCustomView;
	private FrameLayout mCustomViewContainer;
	private WebChromeClient.CustomViewCallback mCustomViewCallback;

	private FrameLayout mContentView;
	private FrameLayout mBrowserFrameLayout;
	private FrameLayout mLayout;
	private ProgressBar progressbar;

	static final String LOGTAG = "HTML5WebView";
	/**
	 * 
	 * 自定义字段
	 */

	public static final String WebUrl = "WebUrl";
	public static final String Title = "Title";

	private void init(Context context) {
		mContext = context;
		Activity mActivity = (Activity) mContext;

		mLayout = new FrameLayout(context);

		mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(mActivity)
				.inflate(R.layout.custom_screen, null);
		mContentView = (FrameLayout) mBrowserFrameLayout
				.findViewById(R.id.main_content);
		mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
				.findViewById(R.id.fullscreen_custom_content);

		mLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
		progressbar = (ProgressBar) mBrowserFrameLayout.findViewById(R.id.progressBar);
		// Configure the webview
		WebSettings s = getSettings();
		s.setBuiltInZoomControls(true);
		s.setCacheMode(WebSettings.LOAD_NO_CACHE);  //设置 缓存模式 
		s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
		s.setUseWideViewPort(true);
		s.setLoadWithOverviewMode(true);
		// s.setSavePassword(true);
		s.setSaveFormData(true);
		s.setJavaScriptEnabled(true);
		mWebChromeClient = new MyWebChromeClient();
		setWebChromeClient(mWebChromeClient);

		setWebViewClient(new MyWebViewClient());

		setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

		// enable navigator.geolocation
		// s.setGeolocationEnabled(true);
		// s.setGeolocationDatabasePath("/data/data/org.itri.html5webview/databases/");

		// enable Web Storage: localStorage, sessionStorage
		s.setDomStorageEnabled(true);

		mContentView.addView(this);
	}

	public Html5Webview(Context context) {
		super(context);
		init(context);
	}

	public Html5Webview(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public Html5Webview(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public FrameLayout getLayout() {
		return mLayout;
	}

	public boolean inCustomView() {
		return (mCustomView != null);
	}

	public void hideCustomView() {
		mWebChromeClient.onHideCustomView();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if ((mCustomView == null) && canGoBack()) {
				goBack();
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	public class TitleHolder {
		public String title;
	}
	private class MyWebViewClient extends WebViewClient {
	  @Override
	public void onPageFinished(final WebView view, String url) {
		// TODO Auto-generated method stub
		super.onPageFinished(view, url);
		
	
		
	}
	}
	private class MyWebChromeClient extends WebChromeClient {
		private Bitmap mDefaultVideoPoster;
		private View mVideoProgressView;

		@Override
		public void onShowCustomView(View view, CustomViewCallback callback) {
			// Log.i(LOGTAG, "here in on ShowCustomView");
			Html5Webview.this.setVisibility(View.GONE);

			// if a view already exists then immediately terminate the new one
			if (mCustomView != null) {
				callback.onCustomViewHidden();
				return;
			}

			mCustomViewContainer.addView(view);
			mCustomView = view;
			mCustomViewCallback = callback;
			mCustomViewContainer.setVisibility(View.VISIBLE);
		}

		@Override
		public void onHideCustomView() {

			if (mCustomView == null)
				return;

			// Hide the custom view.
			mCustomView.setVisibility(View.GONE);

			// Remove the custom view from its container.
			mCustomViewContainer.removeView(mCustomView);
			mCustomView = null;
			mCustomViewContainer.setVisibility(View.GONE);
			mCustomViewCallback.onCustomViewHidden();

			Html5Webview.this.setVisibility(View.VISIBLE);
			Html5Webview.this.goBack();
			// Log.i(LOGTAG, "set it to webVew");
		}

		@Override
		public View getVideoLoadingProgressView() {
			// Log.i(LOGTAG, "here in on getVideoLoadingPregressView");

			if (mVideoProgressView == null) {
				LayoutInflater inflater = LayoutInflater.from(mContext);
				mVideoProgressView = inflater.inflate(R.layout.video_loading,
						null);
			}
			return mVideoProgressView;
		}

		@Override
		public void onReceivedTitle(WebView view, String title) {
			TitleHolder titleHolder = new TitleHolder();
			titleHolder.title = title;
			EventBus.getDefault().post(titleHolder);
		}

		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			((Activity) mContext).getWindow().setFeatureInt(
					Window.FEATURE_PROGRESS, newProgress * 100);
//			 if (newProgress == 100) {
//	                progressbar.setVisibility(GONE);
//	            } else {
//	                if (progressbar.getVisibility() == GONE)
//	                    progressbar.setVisibility(VISIBLE);
//	                progressbar.setProgress(newProgress);
//	            }
//	            super.onProgressChanged(view, newProgress);
		}

		@Override
		public void onGeolocationPermissionsShowPrompt(String origin,
				GeolocationPermissions.Callback callback) {
			callback.invoke(origin, true, false);
		}
		
	}

	static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams(
			ViewGroup.LayoutParams.MATCH_PARENT,
			ViewGroup.LayoutParams.MATCH_PARENT);
}
